/*
 * ThreadRunReference.java
 *
 * Created on April 21, 2007, 6:59 PM
 *
 * Replaces the function of the ReferenceLookup process during the Invokevirtual process flow
 * embedded within the Thread start process.
 *
 * The outputs must be:
 *    NAME
 *    DESCRIPTOR
 *    CLASSHANDLE
 *
 * Since the Run() method doesn't get a MethodRefInfo structure placed into Thread's constant
 * pool by the compiler unless it is referenced somewhere in Thread (it is only called from the
 * native method Start()), we can't look Run up using the ReferenceLookup process.
 *
 * Therefore we manually set the registers to the state they would be had ReferenceLookup been
 * run.
 *
 *      name = "run".hashCode() = 0x1ba8b   
 *      descriptor = "()V".hashCode() = 0x9b75
 * 
 * classhandle is set by looking up the class of objectref ('this' in local variable 0).
 *
 * Notes:
 *
 *   The hashcodes for these strings MUST match the hashcodes generated by ClassPackager. 
 *   ClassPackager uses the Java hashCode() method to generate the hash numbers.
 */

package emr.jvm.process.system;

import emr.jvm.process.JVMProcess;
import emr.jvm.Debug;

import emr.jvm.JVMRuntime; // for the registers
import emr.jvm.memory.MemoryController;
import emr.jvm.memory.nvm.*; // for the nvm
import emr.jvm.memory.ram.*; // for the ram


/**
 *
 * @author Evan Ross
 */
public class ThreadRunReference extends JVMProcess
{
    
    /** Creates a new instance of ThreadRunReference */
    public ThreadRunReference()
    {
        super("ThreadRunReference");
    }
    
    public void runProcess()  
    {
        checkStatus();
        
        // Load currentframe into name
        JVMRuntime.name = MemoryController.readWord(JVMRuntime.currentthread + ObjectThread.CurrentframeOffset);
        
        // Get 'this' from local variable at index 0
        // where the current frame is that of start()
        JVMRuntime.value = MemoryController.readWord( JVMRuntime.name + Frame.LOCAL_VAR_OFFSET + 0 );
        
        // Set the name register to the hascode of "run"
        JVMRuntime.name = 0x1ba8b;
        
        // Set the descriptor register to the hashcode of "()V"
        JVMRuntime.descriptor = 0x9b75;

        //
        // Now get the class of the objectref
        // Overwrite classhandle with actual object's class handle and let Field/MethodLookup do its magic.
        //
        JVMRuntime.classhandle = MemoryController.readWord( JVMRuntime.value + ObjectBase.classReferenceOffset );
        
    }
    
    
}
